<template>
  <div class="login">
    <!-- 星空背景层 -->
    <div class="stars"></div>
    <div class="twinkling"></div>
    <div class="shooting-stars"></div>
    
    <!-- 登录表单 -->
    <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">{{ title }}</h3>
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          size="large"
          auto-complete="off"
          placeholder="账号"
        >
          <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          size="large"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter="handleLogin"
        >
          <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
          v-model="loginForm.code"
          size="large"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter="handleLogin"
        >
          <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img"/>
        </div>
      </el-form-item>
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="large"
          type="primary"
          style="width:100%;"
          @click.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
    
    <!-- 底部版权信息 -->
    <div class="el-login-footer">
      <span>版权所有 © 2025 广西兰逸科技有限责任公司 </span>
    </div>
  </div>
</template>

<script setup>
import { getCodeImg } from "@/api/login"
import Cookies from "js-cookie"
import { encrypt, decrypt } from "@/utils/jsencrypt"
import useUserStore from '@/store/modules/user'

const title = "Triz智慧系统"
const userStore = useUserStore()
const route = useRoute()
const router = useRouter()
const { proxy } = getCurrentInstance()

const loginForm = ref({
  username: "admin",
  password: "admin123",
  rememberMe: false,
  code: "",
  uuid: ""
})

const loginRules = {
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }]
}

const codeUrl = ref("")
const loading = ref(false)
const captchaEnabled = ref(true)
const register = ref(false)
const redirect = ref(undefined)

watch(route, (newRoute) => {
    redirect.value = newRoute.query && newRoute.query.redirect
}, { immediate: true })

function handleLogin() {
  proxy.$refs.loginRef.validate(valid => {
    if (valid) {
      loading.value = true
      if (loginForm.value.rememberMe) {
        Cookies.set("username", loginForm.value.username, { expires: 30 })
        Cookies.set("password", encrypt(loginForm.value.password), { expires: 30 })
        Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 })
      } else {
        Cookies.remove("username")
        Cookies.remove("password")
        Cookies.remove("rememberMe")
      }
      userStore.login(loginForm.value).then(() => {
        const query = route.query
        const otherQueryParams = Object.keys(query).reduce((acc, cur) => {
          if (cur !== "redirect") {
            acc[cur] = query[cur]
          }
          return acc
        }, {})
        router.push({ path: redirect.value || "/", query: otherQueryParams })
      }).catch(() => {
        loading.value = false
        if (captchaEnabled.value) {
          getCode()
        }
      })
    }
  })
}

function getCode() {
  getCodeImg().then(res => {
    captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.img
      loginForm.value.uuid = res.uuid
    }
  })
}

function getCookie() {
  const username = Cookies.get("username")
  const password = Cookies.get("password")
  const rememberMe = Cookies.get("rememberMe")
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password: password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  }
}

getCode()
getCookie()
</script>

<style lang='scss' scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  position: relative;
  overflow: hidden;
  background: linear-gradient(to bottom, #0b0e2a 0%, #1a1f4d 40%, #2a2f5d 100%);
}

/* 星空背景 */
.stars {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  display: block;
  background: url('');
  z-index: 0;
  animation: moveStars 100s linear infinite;
}

/* 星星闪烁效果 */
.twinkling {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  display: block;
  z-index: 1;
  
  &:before, &:after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: transparent;
    animation: twinkle 4s ease-in-out infinite;
  }
  
  &:after {
    animation-delay: 2s;
  }
}

/* 流星效果 */
.shooting-stars {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  display: block;
  z-index: 1;
  
  &:before, &:after, &:nth-child(3):before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    height: 2px;
    background: linear-gradient(to right, transparent, rgba(255, 255, 255, 0.8), transparent);
    transform: rotate(-45deg);
    transform-origin: 0 0;
    animation: shootingStar 10s ease-in infinite;
  }
  
  &:after {
    top: 30%;
    left: 50%;
    animation-delay: 5s;
    animation-duration: 8s;
  }
  
  &:nth-child(3):before {
    top: 80%;
    left: 20%;
    animation-delay: 7s;
    animation-duration: 12s;
  }
}

/* 星星移动动画 */
@keyframes moveStars {
  0% { transform: translate(0, 0); }
  100% { transform: translate(-100px, 100px); }
}

/* 星星闪烁动画 */
@keyframes twinkle {
  0% { opacity: 0.3; }
  50% { opacity: 1; }
  100% { opacity: 0.3; }
}

/* 流星动画 */
@keyframes shootingStar {
  0% {
    transform: rotate(-45deg) translateX(0);
    opacity: 0;
    width: 0;
  }
  10% {
    opacity: 1;
  }
  80% {
    opacity: 1;
    width: 200px;
  }
  100% {
    transform: rotate(-45deg) translateX(1000px);
    opacity: 0;
    width: 300px;
  }
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #fff;
  font-size: 28px;
  font-weight: 600;
  letter-spacing: 1px;
  text-shadow: 0 0 10px rgba(100, 200, 255, 0.5);
  position: relative;
  z-index: 2;
}

.login-form {
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  width: 400px;
  padding: 30px;
  z-index: 2;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  
  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.4);
  }
  
  .el-input {
    height: 48px;
    input {
      height: 48px;
      border-radius: 6px;
      font-size: 14px;
      background: rgba(255, 255, 255, 0.9);
      border: 1px solid rgba(255, 255, 255, 0.3);
    }
  }
  .input-icon {
    height: 47px;
    width: 14px;
    margin-left: 0px;
  }
}

.login-code-img {
  height: 40px;
  padding-left: 12px;
  border-radius: 4px;
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: rgba(255, 255, 255, 0.7);
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 0.5px;
  z-index: 2;
}

.link-type {
  color: #4fc3f7;
  text-decoration: none;
  transition: color 0.2s ease;
  
  &:hover {
    color: #03a9f4;
    text-shadow: 0 0 5px rgba(79, 195, 247, 0.5);
  }
}

.el-button--primary {
  background: linear-gradient(45deg, #4fc3f7, #2196f3);
  border: none;
  transition: all 0.3s ease;
  
  &:hover {
    background: linear-gradient(45deg, #03a9f4, #1976d2);
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(33, 150, 243, 0.4);
  }
  
  &:active {
    transform: translateY(0);
  }
}
</style>